package com.awspaas.user.apps.sts;

import com.actionsoft.bpms.api.ApiUtil;
import com.actionsoft.bpms.commons.at.ExpressionContext;
import com.actionsoft.bpms.commons.at.impl.num.AbsExpression;
import com.actionsoft.sdk.local.SDK;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.awspaas.user.apps.sts.util.CommonUtil;

import java.time.LocalDate;
import java.util.HashMap;

public class IsCsTrustProject extends AbsExpression {
    public IsCsTrustProject(ExpressionContext atContext, String expressionValue) {
        super(atContext, expressionValue);
    }

    public String execute(String expression) {
        SDK.getLogAPI().consoleInfo("获取项目信息同步开始===");
        String projectCode = getParameter(expression, 1);
        HashMap<String, String> param = new HashMap<>();
        LocalDate localDate = LocalDate.now().minusDays(1);
        String localDateToStr = CommonUtil.getLocalDateToStr(localDate);
        param.put("datetime", localDateToStr);
        param.put("project_code", projectCode);//净值型产品
        //产品信息查询
       String responseStr= ApiUtil.callApiInterface("project",param);
        JSONObject jsonObjectRes = JSONObject.parseObject(responseStr);
        JSONObject dataObject = jsonObjectRes.getJSONObject("data");
        String result_num = dataObject.getString("result_num");
        if("0"==result_num){
            return "FALSE";
        }
        JSONArray jsonArray = dataObject.getJSONArray("data");
        if(jsonArray.isEmpty()||jsonArray.size()==0){
            return "FALSE";
        }
        JSONObject jsonObject = jsonArray.getJSONObject(0);
        String specialbusin_type_code = jsonObject.getString("specialbusin_type_code");
        SDK.getLogAPI().consoleInfo("获取最后是否慈善信托的返回结果"+specialbusin_type_code);
        if("6".equals(specialbusin_type_code)){
            return "TRUE";
        }else {
            return "FALSE";
        }
    }
}
